/**
 * 
 */
package com.google.gwt.sample.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

/**
 * @author Prashanth
 *
 */
public class HTMLParser {
	

		
	
	
public static void main(String[] args){
	
		String fileName = "war/output/output.html";
		String encoding = "UTF-8";
		//String inputHTML = readFile("filemakerExport.htm","UTF-8");
		
		  StringBuilder text = new StringBuilder();
		  String NL = System.getProperty("line.separator");
		  Scanner scanner = null;
		try {
			scanner = new Scanner(new FileInputStream(fileName), encoding);
		
		     while (scanner.hasNextLine()){
		       text.append(scanner.nextLine() + NL);
		     }
		  
		String inputHTML = text.toString();
		Parser parser = new Parser();
		parser.setInputHTML(inputHTML);
		parser.setEncoding("UTF-8");    
		NodeList nl = parser.parse(null); 
		NodeList trs = nl.extractAllNodesThatMatch(new TagNameFilter("tr"),true);
		StringBuilder strBuilder = new StringBuilder();
		for(int i=0;i<trs.size();i++) {
		    NodeList nodes = trs.elementAt(i).getChildren();
		    NodeList tds  = nodes.extractAllNodesThatMatch(new TagNameFilter("td"),true);
		    
		    // Do stuff with tds
		    strBuilder.append(tds.asString().trim());
		}
		
		String[] stringArray = strBuilder.toString().trim().split("&nbsp;");
		System.out.println(strBuilder.toString());
		String symbol = stringArray[0];
		String[] priceArray = stringArray[2].split(" ");
		String[] changeArray = stringArray[3].split(" ");
		String[] perChangeArray =  stringArray[5].split(" ");
		String[] dateArray =  stringArray[9].split(":");
		
		String perChange = perChangeArray[2].trim();		
		String date = dateArray[1].trim();
		String price = priceArray[1].trim();
		String change = changeArray[1].trim();
		
		System.out.println("Symbol: "+symbol);
		System.out.println("Price: "+price);
		System.out.println("Change: "+change);
		System.out.println("Percentage Change: "+perChange);
		System.out.println("Record Date: "+date);
		
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ParserException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
	      scanner.close();
	  }
	}
}